using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Action.Common;
using T.FAS.Runtime.Action.SPI;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.DataAccess.Common;

namespace T.FAS.Runtime.DataAccess.Kernel
{
    public class TResultConverter
    {
        private static readonly IDACLogService s_localLogService = DACLogFactory.GetDACLogService();
        public static TResult ConvertToTResultFromCDO(CompositeDataObject CDO, List<DataTable> dataTables, bool IsListResult)
        {
            /*******暂时直接引用plugin，后续需要替换成Action的SDK*******/
            var schema = CDO.BuildSchema();
            schema.ResultType = IsListResult ? DataType.List : DataType.Record;
            try
            {
                TResult tresult = TResultBuilder.BuildTResult(dataTables, schema);
                return tresult;
            }
            catch (Exception e)
            {
                s_localLogService.Write(new
                {
                    ErrorMsg = "调试CDO查询结果转TRecord BUG",
                    CDOMetadata = CDO,
                    Schema = schema,
                    Data = dataTables.Select(dt => new { TableName = dt.TableName, TableData = dt }),
                    IsListResult = IsListResult
                });
                throw new FasException(DataAccessErrorCode.CompositeDataObject.UnableCastToTRecord, CommonStringClassKernel.TResultConverter_ConvertToTResultFromCDO, e);
            }
            
        }

        public static TResult ConvertToTResultFromDO(DataObject DO, List<DataTable> dataTables)
        {
            throw new NotImplementedException();
        }
    }
}
